<template>
  <el-row>
    <el-card>
      <el-form ref="searchForm" :inline="true" :model="searchForm">
        <el-form-item :label="$t('reportModule.conversionFunnel.date')">
          <el-date-picker
            v-model="time"
            type="daterange"
            range-separator="~"
            :start-placeholder="$t('common.startDate')"
            :end-placeholder="$t('common.endDate')"
          />
        </el-form-item>
        <el-form-item :label="$t('reportModule.conversionFunnel.packageName')">
          <el-select v-model="searchForm.packageName" clearable :placeholder="$t('common.select')">
            <el-option
              v-for="item in allPackageData"
              :key="item.value"
              :label="item.desc"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('reportModule.conversionFunnel.version')">
          <el-select v-model="searchForm.appVersionName" clearable :placeholder="$t('common.select')">
            <el-option
              v-for="item in allVersions"
              :key="item.value"
              :label="item.desc"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-row>
          <el-button type="primary" @click="search">
            {{ $t('common.search') }}
          </el-button>
          <el-button @click="reset">
            {{ $t('common.reset') }}
          </el-button>
          <el-button :disabled="!(totalNumber > 0)" @click="exportList">
            {{ $t('common.ex') }}
          </el-button>
        </el-row>
      </el-form>
    </el-card>
    <el-card>
      <el-table
        v-loading="loading"
        :header-cell-style="{background:'#eef1f6',color:'#606266'}"
        :data="tableData"
        border
        :stripe="true"
        style="width: 100%"
      >
        <el-table-column
          fixed="left"
          prop="dateStr"
          :label="tableText['date']"
          min-width="120"
        />
        <el-table-column
          fixed="left"
          prop="registerCount"
          :label="tableText['registerUserCount']"
          min-width="120"
        >
          <template slot-scope="scope">
            {{ scope.row['registerCount'] || 0 }}
          </template>
        </el-table-column>
        <el-table-column
          prop="realNameRate"
          :label="tableText['realNameRate']"
          min-width="150"
        >
          <template slot-scope="scope">
            {{ scope.row['realNameRate'] ? (scope.row['realNameRate'] * 100).toFixed(2) + '%' : 0 }}
          </template>
        </el-table-column>
        <el-table-column
          prop="personalInfoRate"
          :label="tableText['personalInfoRate']"
          min-width="150"
        >
          <template slot-scope="scope">
            {{ scope.row['personalInfoRate'] ? (scope.row['personalInfoRate'] * 100).toFixed(2) + '%' : 0 }}
          </template>
        </el-table-column>
        <el-table-column
          prop="workInfoRate"
          :label="tableText['workInfoRate']"
          min-width="150"
        >
          <template slot-scope="scope">
            {{ scope.row['workInfoRate'] ? (scope.row['workInfoRate'] * 100).toFixed(2) + '%' : 0 }}
          </template>
        </el-table-column>
        <el-table-column
          prop="contactInfoRate"
          :label="tableText['contactInfoRate']"
          min-width="170"
        >
          <template slot-scope="scope">
            {{ scope.row['contactInfoRate'] ? (scope.row['contactInfoRate'] * 100).toFixed(2) + '%' : 0 }}
          </template>
        </el-table-column>
        <el-table-column
          prop="bankInfoRate"
          :label="tableText['bankInfoRate']"
          min-width="190"
        >
          <template slot-scope="scope">
            {{ scope.row['bankInfoRate'] ? (scope.row['bankInfoRate'] * 100).toFixed(2) + '%' : 0 }}
          </template>
        </el-table-column>
        <el-table-column
          prop="orderRate"
          :label="tableText['orderRate']"
          min-width="150"
        >
          <template slot-scope="scope">
            {{ scope.row['orderRate'] ? (scope.row['orderRate'] * 100).toFixed(2) + '%' : 0 }}
          </template>
        </el-table-column>
        <el-table-column
          prop="livingRate"
          :label="tableText['livingRate']"
          min-width="150"
        >
          <template slot-scope="scope">
            {{ scope.row['livingRate'] ? (scope.row['livingRate'] * 100).toFixed(2) + '%' : 0 }}
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination
          :current-page="searchForm.currentPage"
          :page-sizes="[10, 20, 30, 40, 50]"
          :page-size="searchForm.pageSize"
          :total="totalNumber"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
  </el-row>

</template>

<script>
import { formatDate } from '@/utils/method'
import { reportPackages, reportVersions, register2apply, register2applyExport } from '@/api/report'
export default {
  name: 'ConversionFunnel',
  data() {
    return {
      loading: false,
      tableData: [],
      tableText: {},
      totalNumber: 0,
      time: [new Date(), new Date()],
      searchForm: this.params(),
      allPackageData: [],
      allVersions: []
    }
  },
  mounted() {
    this.tableText = this.$t('reportModule.conversionFunnel')
    this.getData()
    this.getAllPackage()
    this.getAllVersions()
  },
  methods: {
    params() {
      return {
        currentPage: 1,
        pageSize: 10,
        registerTimeStart: null,
        registerTimeEnd: null,
        packageName: '',
        appVersionName: ''
      }
    },
    getAllPackage() {
      reportPackages()
        .then(res => {
          if (res.success) {
            this.allPackageData = [
              {
                desc: this.$t('common.all'),
                value: ''
              }
            ]
            res.data.forEach(v => {
              this.allPackageData.push({
                desc: v,
                value: v
              })
            })
          } else {
            this.$message.error(res.message)
          }
        })
        .catch(err => {
          this.$message.error(err.message)
        })
    },
    getAllVersions() {
      reportVersions()
        .then(res => {
          if (res.success) {
            this.allVersions = [
              {
                desc: this.$t('common.all'),
                value: ''
              }
            ]
            res.data.forEach(v => {
              this.allVersions.push({
                desc: v,
                value: v
              })
            })
          } else {
            this.$message.error(res.message)
          }
        })
        .catch(err => {
          this.$message.error(err.message)
        })
    },
    search() {
      this.searchForm.currentPage = 1
      this.getData()
    },
    reset() {
      this.searchForm = this.params()
      this.time = [new Date(), new Date()]
      this.getData()
    },
    searchData() {
      const data = this.searchForm
      if (this.time && this.time.length > 1) {
        data['registerTimeStart'] = formatDate(new Date(this.time[0]), 'yyyy-MM-dd') + ' 00:00:00'
        data['registerTimeEnd'] = formatDate(new Date(this.time[1]), 'yyyy-MM-dd') + ' 23:59:59'
      } else {
        data['registerTimeStart'] = null
        data['registerTimeEnd'] = null
      }
      return data
    },
    getData() {
      this.loading = true
      register2apply(this.searchData())
        .then(res => {
          this.loading = false
          if (res.success) {
            this.tableData = res.data
            this.totalNumber = res.page ? res.page['totalNumber'] : 0
          } else {
            this.$message.error(res.message)
          }
        })
        .catch(err => {
          this.loading = false
          this.$message.error(err.message)
        })
    },
    handleSizeChange(val) {
      this.searchForm.pageSize = val
      this.getData()
    },
    handleCurrentChange(val) {
      this.searchForm.currentPage = val
      this.getData()
    },
    exportList() {
      register2applyExport(this.searchData())
    }
  }
}
</script>

<style lang="scss" scoped>
.el-card{
  margin: 10px;
}
</style>
